//
//  SDBaseTableViewController.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDBaseTableViewController.h"
#import "SDLoadingView.h"
#import "SDAlertManagement.h"
#import "SDUIAutoLayout.h"

@interface SDBaseTableViewController ()<SDNetworkErrorViewDelegate>

@end

@implementation SDBaseTableViewController

//- (UIRectEdge)edgesForExtendedLayout
//{
//    return UIRectEdgeNone;
//}

- (BOOL)automaticallyAdjustsScrollViewInsets
{
    return NO;
}

- (void)dealloc
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    BOOL barHidden = [self navigationBarHidden];
    self.view.backgroundColor = [UIColor colorWithRGB:ColorAllViewsBackground];
    _tableView = [[SDTableView alloc]initWithFrame:autoRectTableViewHaveBar(!barHidden)];
    //_tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    
    if([self isNeedReadyLoadingInDidLoad])
        [self readyLoading];
    
    //    [self initGestureRecognizer];
}

- (void)initGestureRecognizer
{
    UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeCancel:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
    
    UIPanGestureRecognizer* gesture = nil;
    if( [SDAppUtils currentIOSVersion] > 4.9 )
        gesture = _tableView.panGestureRecognizer;
    else
    {
        for (UIPanGestureRecognizer* tpgesture in _tableView.gestureRecognizers) {
            if( [tpgesture isKindOfClass:[UIPanGestureRecognizer class]] )
            {
                gesture = tpgesture;
                break;
            }
        }
    }
    
    [gesture requireGestureRecognizerToFail:swipe];
}

- (void)swipeCancel:(UISwipeGestureRecognizer*)gesture
{
    [self clickReturn:nil];
}

- (void)clickReturn:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - loading tips
- (void)readyLoading
{
    _tableView.hidden = TRUE;
    
    if ([self respondsToSelector:@selector(loadingFrameOfView)])
    {
        CGRect rc = [self loadingFrameOfView];
        [SDLoadingView showLoadingWithSuperView:self.view frame:rc withAnimated:NO];
    }
    else
    {
        [SDLoadingView showLoadingWithSuperView:self.view withAnimated:NO];
    }
}

- (BOOL) isNeedReadyLoadingInDidLoad
{
    return YES;
}

- (BOOL)isDataEmpty
{
    return YES;
}

- (void) showEmpty
{
}

- (void)removeEmpty
{
    
}

- (void)finishLoading
{
    _tableView.scrollEnabled = YES;
    [SDLoadingView hideLoadingFromSuperView:self.view withAnimted:NO];
    [SDNetworkErrorView hideBlankPageErrorFrom:self.tableView];
    [_tableView finishedLoading];
    
    if( [self isDataEmpty] )
    {
        _tableView.hidden = YES;
        [self showEmpty];
    }
    else
    {
        [self removeEmpty];
        _tableView.hidden = NO;
    }
}

- (void)failedLoading
{
    [SDLoadingView hideLoadingFromSuperView:self.view withAnimted:NO];
    [_tableView finishedLoading];
    if( [self isDataEmpty] )
    {
        [self removeEmpty];
        _tableView.hidden = NO;
        _tableView.scrollEnabled = NO;
        
        SDNetworkErrorView* networkView = [SDNetworkErrorView showBlankPageViewTo:self.tableView];
        networkView.backgroundColor = self.view.backgroundColor;
        networkView.delegate = self;
        
        if ([self respondsToSelector:@selector(failedLoadingFrameOfView)])
        {
            CGRect rc = [self failedLoadingFrameOfView];
            networkView.frame = rc;
        }
    }
    else if( self.view.window )
    {
        _tableView.hidden = NO;
        [SDAlertManagement showNetworkErrorAlert:kNoNetWorkTipWord];
    }
}

- (void)failedLoadingWithWarning:(NSString *)warning
{
    [SDLoadingView hideLoadingFromSuperView:self.view withAnimted:NO];
    [_tableView finishedLoading];
    if( self.view.window )
    {
        [SDAlertManagement showNetworkErrorAlert:warning];
    }
}


- (void)simulationRefreshOfPullDown
{
    if(_tableView)
        [_tableView forceLoad];
}

- (void)networkErrorReload
{
    [self readyLoading];
    if( [self conformsToProtocol:@protocol(SDTableViewDelegate)] )
        [self performSelector:@selector(tableViewShouldRefreshData:) withObject:_tableView];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_tableView scrollViewDidScroll];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [_tableView scrollViewDidEndDragging];
}


@end
